﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;

namespace Icms.WebUI.Controllers
{
    public class AjaController : Controller
    {
        private readonly IUniRepo u;

        public AjaController(IUniRepo u)
        {
            this.u = u;
        }

        public ActionResult GetParTitle(Guid id)
        {
            return Content(u.Get<Parameter>(id).Title);
        }

        public ActionResult GetPerson(Guid id)
        {
            return Json(u.Get<Person>(id));
        }

        public ActionResult GetTemplateContent(Guid? id)
        {
            if(id.HasValue)
            return Json(u.Get<Template>(id.Value).Content);
            return new EmptyResult();
        }

        public ActionResult GetCleditorItems(Guid caseId, Guid? hearingId)
        {
            IList<string> vals = new List<string>();
            var c = u.Get<Case>(caseId);
            vals.Add(c.Number);
            vals.Add(c.CreatedDate.ToShortDateString());
            vals.Add(c.Court.Title);
            vals.Add(c.Court.Region.Title);

            foreach (var p in c.Parties)
            {
                vals.Add(p.Person.FirstName + " " + p.Person.LastName);
            }

            if (hearingId.HasValue) vals.Add(u.Get<Hearing>(hearingId.Value).Type.Title);

            return Json(vals);
        }
    }
}